<?php

/**
 * This is the model class for table "tbl_doitac".
 *
 * The followings are the available columns in table 'tbl_doitac':
 * @property integer $id
 * @property string $ten
 * @property string $ma_so
 * @property string $dia_chi
 * @property string $dien_thoai
 * @property integer $id_loai_hinh_kinhdoanh
 * @property string $mo_ta
 * @property string $website
 * @property integer $cam_ket
 * @property integer $chi_phi_dv
 * @property string $so_hop_dong
 * @property integer $tong_doanh_thu
 * @property integer $tong_chi_phi
 * @property integer $da_thanh_toan
 * @property integer $chua_thanh_toan
 * @property string $ngay_vao_so
 * @property double $phan_tram
 */
class Doitac extends CActiveRecord
{
	public $text_loaihinh;
	public $khachhang;
	public $so_luong;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_doitac';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ten, ma_so, dia_chi, dien_thoai, id_loai_hinh_kinhdoanh,mo_ta, website, so_hop_dong, ngay_vao_so, ', 'required','message'=>'{attribute} chưa nhập dữ liệu'),
			array('id_loai_hinh_kinhdoanh, tong_doanh_thu, tong_chi_phi, da_thanh_toan, chua_thanh_toan', 'numerical', 'integerOnly'=>true,'message'=>'{attribute} phải là số'),
			array('cam_ket,chi_phi_dv', 'numerical', 'max'=>100,'tooBig'=>'{attribute} phải nhỏ hơn 100'),
			array('ten', 'length', 'max'=>150),
			array('ma_so, dien_thoai, so_hop_dong', 'length', 'max'=>50),
			array('website', 'length', 'max'=>100),
			array('ma_so,so_hop_dong', 'unique', 'message'=>'{attribute} đã được sử dụng'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, ten,text_loaihinh, ma_so, dia_chi, dien_thoai, id_loai_hinh_kinhdoanh, mo_ta, website, cam_ket, so_hop_dong, tong_doanh_thu, tong_chi_phi, da_thanh_toan, chua_thanh_toan, ngay_vao_so,tong_chiet_khau', 'safe', 'on'=>'search'),
		);
	}
	public static function GetSoTienChuaThanhToan($value)
	{
		$model = DoitacKhachhang::model()->findAll("id_doitac = {$value->id} and trangthai_thanhtoan_dt != 2");
		$tien = 0;
		foreach ($model as $key ) {
			if ($key->chua_thanh_toan_dt > 0) {
				$tien += $key->chua_thanh_toan_dt;
			}
		}
		if ($tien < 0) {
			$tien = 0;
		}
		return $tien;
	}
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'kinhdoanh'=> array(self::BELONGS_TO,'Loaihinhkinhdoanh','id_loai_hinh_kinhdoanh'),
			'hoadon'=> array(self::HAS_MANY,'DoitacKhachhang','id_doitac'),
		);
	}
	public static function getSotienthanhtoan($arrs)
	{
		$tongtien = 0;
		foreach ( $arrs as $key) {
			$hoadon = DoitacKhachhang::model()->findByPk($key);
			if ($hoadon != null) {
				$tongtien += $hoadon->chua_thanh_toan_dt;
			}
		}
		return $tongtien;
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'ten' => 'Tên đối tác',
			'ma_so' => 'Mã số đối tác',
			'dia_chi' => 'Địa chỉ',
			'dien_thoai' => 'Điện thoại',
			'id_loai_hinh_kinhdoanh' => 'Loại hình kinh doanh',
			'mo_ta' => 'Mô tả',
			'website' => 'Website',
			'cam_ket' => 'Cam kêt',
			'so_hop_dong' => 'Số hợp đồng',
			'tong_doanh_thu' => 'Tổng doanh thu',
			'tong_chi_phi' => 'Tổng chi phí',
			'da_thanh_toan' => 'Đã thanh toán',
			'chua_thanh_toan' => 'Chưa thanh toán',
			'ngay_vao_so' => 'Ngày vào sổ',
			'so_luong'=>'Số lượng triết xuất',
			'khachhang'=>'Khách hàng',
			'chi_phi_dv'=>'Chi phí dịch vụ',
			'tong_chiet_khau'=>'Tổng chiết khấu',
		);
	}
	public static function createExcel($model)
	{
		if ($model != null) {
				error_reporting(E_ALL);
				ini_set('display_errors', TRUE);
				ini_set('display_startup_errors', TRUE);
				Yii::import('ext.Excel.XPHPExcel');		
				$objPHPExcel = XPHPExcel::createPHPExcel();
				$objPHPExcel->getProperties()->setCreator("Trieu nhu")
											 ->setLastModifiedBy("Trieu nhu")
											 ->setTitle("Trieu nhu")
											 ->setSubject("Trieu nhu")
											 ->setDescription("Trieu nhu.")
											 ->setKeywords("Trieu nhu")
											 ->setCategory("Trieu nhu");
				$objPHPExcel->getActiveSheet()->setCellValue('A1',"Tên đối tác");
				$objPHPExcel->getActiveSheet()->setCellValue('B1',$model->ten);

				$objPHPExcel->getActiveSheet()->setCellValue('A2',"Mã số đối tác");
				$objPHPExcel->getActiveSheet()->setCellValue('B2',$model->ma_so);				
				$objPHPExcel->getActiveSheet()->setCellValue('A3',"Địa chỉ");
				$objPHPExcel->getActiveSheet()->setCellValue('B3',$model->dia_chi);
				$objPHPExcel->getActiveSheet()->setCellValue('A4',"Điện thoại");
				$objPHPExcel->getActiveSheet()->setCellValue('B4',$model->dien_thoai);
				$objPHPExcel->getActiveSheet()->setCellValue('A5',"Website");
				$objPHPExcel->getActiveSheet()->setCellValue('B5',$model->website);
				$objPHPExcel->getActiveSheet()->setCellValue('A6',"Cam kết chiết khấu");
				$objPHPExcel->getActiveSheet()->setCellValue('B6',$model->cam_ket);
				$objPHPExcel->getActiveSheet()->setCellValue('A7',"Loại hình kinh doanh");
				$objPHPExcel->getActiveSheet()->setCellValue('B7',$model->kinhdoanh->ten_loaihinh);
				$objPHPExcel->getActiveSheet()->setCellValue('A8',"Số hợp đồng");
				$objPHPExcel->getActiveSheet()->setCellValue('B8',$model->so_hop_dong);
				$objPHPExcel->getActiveSheet()->setCellValue('A9',"Tổng doanh thu");
				$objPHPExcel->getActiveSheet()->setCellValue('B9',getMoney($model->tong_doanh_thu));
				$objPHPExcel->getActiveSheet()->setCellValue('A10',"Chi phí");
				$objPHPExcel->getActiveSheet()->setCellValue('B10',getMoney($model->tong_chi_phi));
				$objPHPExcel->getActiveSheet()->setCellValue('A11',"Đã thanh toán");
				$objPHPExcel->getActiveSheet()->setCellValue('B11',getMoney($model->da_thanh_toan));
				$objPHPExcel->getActiveSheet()->setCellValue('A12',"Chưa thanh toán");
				$objPHPExcel->getActiveSheet()->setCellValue('B12',getMoney($model->chua_thanh_toan));
						// Set active sheet index to the first sheet, so Excel opens this as the first sheet
				$objPHPExcel->setActiveSheetIndex(0);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('A')
						    ->setWidth(20);
				$objPHPExcel->getActiveSheet()
						    ->getColumnDimension('B')
						    ->setWidth(20);
				$filename = toSlug(stripVietnamese($model->ten)).'-'.strtotime(date("Y-m-d H:i:s")).'.xls';
				header('Content-Type: application/vnd.ms-excel');
				header("Content-Disposition: attachment;filename={$filename}");
				header('Cache-Control: max-age=0');

				$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
				$objWriter->save('php://output');
		}
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('ten',$this->ten,true);
		$criteria->compare('ma_so',$this->ma_so,true);
		$criteria->compare('dia_chi',$this->dia_chi,true);
		$criteria->compare('dien_thoai',$this->dien_thoai,true);
		$criteria->compare('id_loai_hinh_kinhdoanh',$this->id_loai_hinh_kinhdoanh);
		$criteria->compare('mo_ta',$this->mo_ta,true);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('cam_ket',$this->cam_ket);
		$criteria->compare('so_hop_dong',$this->so_hop_dong,true);
		$criteria->compare('tong_doanh_thu',$this->tong_doanh_thu);
		$criteria->compare('tong_chi_phi',$this->tong_chi_phi);
		$criteria->compare('da_thanh_toan',$this->da_thanh_toan);
		$criteria->compare('chua_thanh_toan',$this->chua_thanh_toan);
		$criteria->compare('ngay_vao_so',$this->ngay_vao_so,true);
		$criteria->compare('tong_chiet_khau',$this->ngay_vao_so,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
	            'pageSize'=>30,
	        ),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Doitac the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
